#!/bin/bash

# 检查是否以root权限运行
if [ "$EUID" -ne 0 ]; then
    echo "请以root权限运行此脚本（使用sudo）"
    exit 1
fi

# 替换Ubuntu 24.04为阿里云源
echo "正在替换阿里云源..."
mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse" > /etc/apt/sources.list

# 更新软件包列表
echo "正在更新软件包列表..."
apt update

# 升级所有已安装的软件包
echo "正在升级所有软件包..."
apt upgrade -y

# 升级系统到最新版本（例如24.04.6）
echo "正在升级系统到最新版本..."
apt dist-upgrade -y

# 安装git和zsh
echo "正在安装git和zsh..."
apt install -y git zsh

# 获取实际用户的家目录
if [ -n "$SUDO_USER" ]; then
    USER_HOME=$(eval echo ~${SUDO_USER})
    ACTUAL_USER="$SUDO_USER"
else
    USER_HOME="$HOME"
    ACTUAL_USER=$(whoami)
fi

# 安装ohmyzsh
echo "正在安装ohmyzsh..."
# 下载安装脚本并为目标用户安装
wget -O /tmp/install_ohmyzsh.sh https://gitee.com/allenjia09/ohmyzsh/raw/master/tools/install.sh
chmod +x /tmp/install_ohmyzsh.sh

# 为目标用户安装 Oh My Zsh
if [ -n "$SUDO_USER" ]; then
    sudo -u $SUDO_USER sh -c 'RUNZSH=no CHSH=no /tmp/install_ohmyzsh.sh'
else
    RUNZSH=no CHSH=no /tmp/install_ohmyzsh.sh
fi

# 清理临时文件
rm -f /tmp/install_ohmyzsh.sh

# 安装zsh插件
echo "正在安装zsh插件..."
ZSH_CUSTOM="$USER_HOME/.oh-my-zsh/custom"

# 确保插件目录存在
mkdir -p "$ZSH_CUSTOM/plugins"

# 安装zsh-autosuggestions
echo "安装zsh-autosuggestions插件..."
if [ -n "$SUDO_USER" ]; then
    sudo -u $SUDO_USER git clone https://gitee.com/allenjia09/zsh-autosuggestions.git "$ZSH_CUSTOM/plugins/zsh-autosuggestions" --depth=1 || sudo -u $SUDO_USER git clone https://github.com/zsh-users/zsh-autosuggestions.git "$ZSH_CUSTOM/plugins/zsh-autosuggestions" --depth=1
else
    git clone https://gitee.com/allenjia09/zsh-autosuggestions.git "$ZSH_CUSTOM/plugins/zsh-autosuggestions" --depth=1 || git clone https://github.com/zsh-users/zsh-autosuggestions.git "$ZSH_CUSTOM/plugins/zsh-autosuggestions" --depth=1
fi

# 安装zsh-syntax-highlighting
echo "安装zsh-syntax-highlighting插件..."
if [ -n "$SUDO_USER" ]; then
    sudo -u $SUDO_USER git clone https://gitee.com/allenjia09/zsh-syntax-highlighting.git "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" --depth=1 || sudo -u $SUDO_USER git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" --depth=1
else
    git clone https://gitee.com/allenjia09/zsh-syntax-highlighting.git "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" --depth=1 || git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" --depth=1
fi

# 安装zsh-completions
echo "安装zsh-completions插件..."
if [ -n "$SUDO_USER" ]; then
    sudo -u $SUDO_USER git clone https://gitee.com/allenjia09/zsh-completions.git "$ZSH_CUSTOM/plugins/zsh-completions" --depth=1
else
    git clone https://gitee.com/allenjia09/zsh-completions.git "$ZSH_CUSTOM/plugins/zsh-completions" --depth=1
fi

echo "基础环境配置完成！"

# 配置zsh
echo "正在配置zsh..."

# 检查.zshrc文件是否存在，如果不存在则创建
echo "检查并配置.zshrc文件..."
if [ ! -f "$USER_HOME/.zshrc" ]; then
    echo "创建.zshrc文件..."
    touch "$USER_HOME/.zshrc"
    # 添加基本的zsh配置
    cat > "$USER_HOME/.zshrc" << 'EOF'
# 设置ZSH目录
ZSH="$HOME/.oh-my-zsh"

# 设置ZSH主题
ZSH_THEME="robbyrussell"

# 启用的插件
plugins=(git zsh-autosuggestions zsh-syntax-highlighting zsh-completions)

# 初始化Oh My Zsh
source $ZSH/oh-my-zsh.sh

# 自定义设置
# 设置历史命令大小
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000

# 启用共享历史命令
setopt share_history

# 提高终端响应速度
DISABLE_UPDATE_PROMPT=true
DISABLE_AUTO_UPDATE=true
EOF
else
    # 修改已有的.zshrc文件，添加插件和设置ZSH变量
    echo "修改现有.zshrc文件，添加插件和设置ZSH变量..."
    
    # 检查ZSH变量是否已设置，如果未设置则添加
    if ! grep -q "^ZSH=" "$USER_HOME/.zshrc"; then
        sed -i "1i ZSH=\"$USER_HOME/.oh-my-zsh\"" "$USER_HOME/.zshrc"
    fi
    
    # 检查plugins行是否存在，如果不存在则添加
    if ! grep -q "^plugins=" "$USER_HOME/.zshrc"; then
        echo 'plugins=(git zsh-autosuggestions zsh-syntax-highlighting zsh-completions)' >> "$USER_HOME/.zshrc"
    else
        sed -i 's/plugins=(.*)/plugins=(git zsh-autosuggestions zsh-syntax-highlighting zsh-completions)/g' "$USER_HOME/.zshrc"
    fi
    
    # 修复Oh My Zsh的source行路径
    if grep -q "^source /oh-my-zsh.sh" "$USER_HOME/.zshrc"; then
        sed -i 's|^source /oh-my-zsh.sh|source $ZSH/oh-my-zsh.sh|g' "$USER_HOME/.zshrc"
        echo "已修复Oh My Zsh的source行路径"
    fi
    
  
fi

# 设置文件权限
if [ -n "$SUDO_USER" ]; then
    chown -R $SUDO_USER:$SUDO_USER "$USER_HOME/.oh-my-zsh"
    chown $SUDO_USER:$SUDO_USER "$USER_HOME/.zshrc"
    # 如果存在.zshrc备份文件，也设置权限
    [ -f "$USER_HOME/.zshrc.pre-oh-my-zsh" ] && chown $SUDO_USER:$SUDO_USER "$USER_HOME/.zshrc.pre-oh-my-zsh"
fi

# 设置zsh为默认shell
echo "设置zsh为默认shell..."
if [ -n "$SUDO_USER" ]; then
    chsh -s $(which zsh) $SUDO_USER
else
    chsh -s $(which zsh)
fi

echo -e "\n安装完成！请注销并重新登录以使用zsh."

echo -e "\n使用说明："
echo "1. 重新登录后，zsh将自动启动"
echo "2. zsh-autosuggestions 会根据历史命令提供自动建议"
echo "3. zsh-syntax-highlighting 会对命令进行语法高亮"
echo "4. zsh-completions 提供更多命令补全功能"

# 脚本执行完成后自动启动zsh
# 确保以正确的用户身份启动zsh
if [ -n "$SUDO_USER" ]; then
    echo -e "\n正在为用户 $SUDO_USER 启动zsh终端..."
    sudo -u $SUDO_USER -H zsh
else
    echo -e "\n正在启动zsh终端..."
    zsh
fi

# 添加提示信息：如果首次进入zsh失败，或者source ~/.zshrc失败，请手动键入zsh进入，之后不需要
echo -e "\n注意：如果首次进入zsh失败，或者source ~/.zshrc失败，请手动键入zsh进入，之后不需要。"